package com.service;

import static com.def.GenericDef.E_XPBISDEPT_ALREADY_EXIST;
import static com.def.GenericDef.E_XPBISDEPT_NOT_FOUND_DEPT;
import static com.def.GenericDef.E_XPUASGRP_OTHER_ERROR;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDeptDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_dept;
import com.tx.Xpbisdept.XpbisdeptI;
import com.tx.Xpbisdept.XpbisdeptIA;
import com.tx.Xpbisdept.XpbisdeptO;
import com.tx.Xpbisdept.XpbisdeptOA;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpbisdeptService")
public class XpbisdeptService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpbisdeptService.class);
	private static String TX_NAME = "XPBISDEPT";
	@Autowired
	private XpbisdeptI xpbisdeptI;
//	private XpbisdeptIA xpbisdeptIA;
	@Autowired
	private XpbisdeptO xpbisdeptO;
	private XpbisdeptOA oary;
	@Autowired
	private IBisDeptDao bisDeptDao;
	@Autowired
	private MqOpe mqOpe;
	
	private Timestamp gEvtTimestamp;
	
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		String reply ="";
		gEvtTimestamp = Util.getCurrentTimestamp();
		try{
			xpbisdeptI = (XpbisdeptI) mqOpe.inStr2Obj(strInMsg,xpbisdeptI);
			xpbisdeptO.setTrx_id("XPBISDEPT");
			xpbisdeptO.setType_id("O");
			String actionFlg = xpbisdeptI.getAction_flg();
			if(actionFlg.equals("Q")){
				getDeptProc( );
			}else if( actionFlg.equals("A") ){
				AddDeptProc( );
			}else if( actionFlg.equals("U") ){
				UpdateDeptProc();
			}else if( actionFlg.equals("D")){
				DeleteDeptProc( );
			}
			if( RETURN_CODE_OK.equals(xpbisdeptO.getRtn_code()) ){
				bisDeptDao.flushCurSession();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			
		}catch( Exception ex ){
			logger.error(Util.stackTraceToString(ex));
			xpbisdeptO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisdeptO.setRtn_mesg(ex.toString());
			
		}finally{
			reply = mqOpe.obj2Str(xpbisdeptO);
			logger.info(reply);
			return reply;
		}
		
	}
	public void getDeptProc(){
		List<XpbisdeptOA> oaryList = new ArrayList<XpbisdeptOA>();
		try{
			long tbl_cnt = xpbisdeptI.getTbl_cnt();
			String dept_id = "";
			if( tbl_cnt == 1){
				XpbisdeptIA iary = xpbisdeptI.getIary().get(0);
				dept_id = iary.getDept_id();
				Bis_dept bis_dept = bisDeptDao.get(dept_id);
				if( bis_dept == null){
					xpbisdeptO.setRtn_code(RETURN_CODE_UNKNOWN);
					xpbisdeptO.setRtn_mesg("部门代码["+ dept_id +"]不存在，请核查!");
					return ;
				}else{
					oary.setDept_id(bis_dept.getDept_id());
					oary.setDept_name(bis_dept.getDept_name());
					oary.setDept_e_name(bis_dept.getDept_e_name());
					oary.setDept_lvl(bis_dept.getDept_lvl());
					oary.setDept_s_name(bis_dept.getDept_s_name());
					oary.setUp_dept_id(bis_dept.getUp_dept_id());
					oary.setEvt_timestamp(bis_dept.getEvt_timestamp());
					oary.setEvt_usr(bis_dept.getEvt_usr());
					oaryList.add(oary);
				}
			}else{
				if( dept_id.equals("")){
					List<Bis_dept> bisDeptList = bisDeptDao.loadAll(); 
					if( bisDeptList != null){
						for(Bis_dept bis_dept : bisDeptList ){
							XpbisdeptOA oary = new XpbisdeptOA();
							oary.setDept_id(bis_dept.getDept_id());
							oary.setDept_name(bis_dept.getDept_name());
							oary.setDept_e_name(bis_dept.getDept_e_name());
							oary.setDept_lvl(bis_dept.getDept_lvl());
							oary.setDept_s_name(bis_dept.getDept_s_name());
							oary.setUp_dept_id(bis_dept.getUp_dept_id());
							oary.setValid_flg(bis_dept.getValid_flg());
							oary.setEvt_timestamp(bis_dept.getEvt_timestamp());
							oary.setEvt_usr(bis_dept.getEvt_usr());
							oaryList.add(oary);
						}
					}
				}
			}
		}catch(Exception ex){
			xpbisdeptO.setRtn_code(E_XPUASGRP_OTHER_ERROR +"");
			xpbisdeptO.setRtn_mesg(ex.getMessage());
			logger.error(Util.stackTraceToString(ex));
			return ;
		}
		xpbisdeptO.setRtn_code( RETURN_CODE_OK );
		xpbisdeptO.setRtn_mesg( RETURN_MESG_OK );
		xpbisdeptO.setOary(oaryList);
		xpbisdeptO.setTbl_cnt(oaryList.size());
	}
	public void AddDeptProc(){
		long tbl_cnt = xpbisdeptI.getTbl_cnt();
		for(int i=0;i<tbl_cnt;i++){
			XpbisdeptIA iary = xpbisdeptI.getIary().get(0);
			String dept_id = iary.getDept_id();
			Bis_dept bis_dept = bisDeptDao.get(dept_id);
			if( bis_dept != null){
				xpbisdeptO.setRtn_code(E_XPBISDEPT_ALREADY_EXIST+"");
				xpbisdeptO.setRtn_mesg("部门代码["+dept_id+"] 已经存在! ");
				return ;
			}
			bis_dept = new Bis_dept();
			bis_dept.setDept_id(iary.getDept_id());
			bis_dept.setUnq_seq_id(GUIDGenerator.javaGUID());
			bis_dept.setDept_e_name(iary.getDept_e_name());
			bis_dept.setDept_name(iary.getDept_name());
			bis_dept.setDept_lvl(iary.getDept_lvl());
			bis_dept.setDept_s_name(iary.getDept_s_name());
			bis_dept.setEvt_usr(iary.getEvt_usr());
			bis_dept.setEvt_timestamp(gEvtTimestamp);
			bisDeptDao.save(bis_dept);
		}
		xpbisdeptO.setRtn_code(RETURN_CODE_OK);
		xpbisdeptO.setRtn_mesg(RETURN_MESG_OK);
	}
	public void UpdateDeptProc(){
		long tbl_cnt = xpbisdeptI.getTbl_cnt();
		for(int i=0;i<tbl_cnt;i++){
			XpbisdeptIA iary = xpbisdeptI.getIary().get(i);
			String dept_id = iary.getDept_id();
			Bis_dept bis_dept = bisDeptDao.get(dept_id);
			if( bis_dept == null){
				xpbisdeptO.setRtn_code(E_XPBISDEPT_NOT_FOUND_DEPT +"");
				xpbisdeptO.setRtn_mesg("部门代码 ["+dept_id+"]找不到,无法更新!");
				return ;
			}
			bis_dept.setDept_e_name(iary.getDept_e_name());
			bis_dept.setDept_name(iary.getDept_name());
			bis_dept.setDept_lvl(iary.getDept_lvl());
			bis_dept.setDept_s_name(iary.getDept_s_name());
			bis_dept.setEvt_usr(iary.getEvt_usr());
			bis_dept.setValid_flg(iary.getValid_flg());
			bis_dept.setEvt_timestamp(gEvtTimestamp);
			bisDeptDao.update(bis_dept);
		}
		xpbisdeptO.setRtn_code( RETURN_CODE_OK );
		xpbisdeptO.setRtn_mesg( RETURN_MESG_OK );
	}
	public void DeleteDeptProc(){
		long tbl_cnt = xpbisdeptI.getTbl_cnt();
		for(int i=0;i<tbl_cnt;i++){
			XpbisdeptIA iary = xpbisdeptI.getIary().get(i);
			String dept_id = iary.getDept_id();
			Bis_dept bis_dept = bisDeptDao.get(dept_id);
			if( bis_dept == null){
				xpbisdeptO.setRtn_code(E_XPBISDEPT_NOT_FOUND_DEPT +"");
				xpbisdeptO.setRtn_mesg("部门代码 ["+dept_id+"]找不到,无法删除!");
				return ;
			}
			bisDeptDao.delete(bis_dept);
		}
		xpbisdeptO.setRtn_code( RETURN_CODE_OK );
		xpbisdeptO.setRtn_mesg( RETURN_MESG_OK );
	}
}
